/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#ifndef EXAMPLES_MATRIX_MATMUL_SPLITK_OP_KERNEL_MATMUL_SPLITK_CUSTOM_IMPL_H
#define EXAMPLES_MATRIX_MATMUL_SPLITK_OP_KERNEL_MATMUL_SPLITK_CUSTOM_IMPL_H
#include "kernel_operator.h"
#include "lib/matmul_intf.h"

namespace MatmulSplitKCustom {
template <typename aType, typename bType, typename cType, typename biasType>
class MatmulSplitkKernel {
    private:
        constexpr static MatmulConfig MM_CFG = GetNormalConfig();
    public:
        __aicore__ inline MatmulSplitkKernel(){};
        __aicore__ inline void Init(GM_ADDR a, GM_ADDR b, GM_ADDR bias, GM_ADDR c, GM_ADDR workspace, const TCubeTiling& tiling);
        __aicore__ inline void Process(AscendC::TPipe* pipe);
        AscendC::Matmul<
            AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, aType>,
            AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, bType>,
            AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, cType>,
            AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, biasType>, MM_CFG> matmulObj;

    private:
        __aicore__ inline void CalcOffset(int32_t blockIdx, const TCubeTiling& tiling, int32_t& offsetA, int32_t& offsetB,
                                          int32_t& offsetC, int32_t& offsetBias);

        AscendC::GlobalTensor<aType> aGlobal;
        AscendC::GlobalTensor<bType> bGlobal;
        AscendC::GlobalTensor<cType> cGlobal;
        AscendC::GlobalTensor<biasType> biasGlobal;
        TCubeTiling tiling;
};

template <typename aType, typename bType, typename cType, typename biasType>
__aicore__ inline void MatmulSplitkKernel<aType, bType, cType, biasType>::Init(
    GM_ADDR a, GM_ADDR b, GM_ADDR bias, GM_ADDR c, GM_ADDR workspace, const TCubeTiling& tiling)
{
    this->tiling = tiling;
    aGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ aType*>(a), tiling.M * tiling.Ka);
    bGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ bType*>(b), tiling.Kb * tiling.N);
    cGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ cType*>(c), tiling.M * tiling.N);
    biasGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ biasType*>(bias), tiling.N);

    // clear gm
    InitGlobalMemory(cGlobal, tiling.M * tiling.N, (cType)0);

    int32_t offsetA = 0;
    int32_t offsetB = 0;
    int32_t offsetC = 0;
    int32_t offsetBias = 0;
    CalcOffset(AscendC::GetBlockIdx(), tiling, offsetA, offsetB, offsetC, offsetBias);
    aGlobal = aGlobal[offsetA];
    bGlobal = bGlobal[offsetB];
    cGlobal = cGlobal[offsetC];
    biasGlobal = biasGlobal[offsetBias];
    if(GetSysWorkSpacePtr() == nullptr){
        return;
    }
}

template <typename aType, typename bType, typename cType, typename biasType>
__aicore__ inline void MatmulSplitkKernel<aType, bType, cType, biasType>::Process(AscendC::TPipe* pipe)
{
    if (matmul::GetBlockIdx() >= tiling.usedCoreNum) {
        return;
    }
    matmulObj.SetTensorA(aGlobal);
    matmulObj.SetTensorB(bGlobal);
    if (tiling.isBias) {
        matmulObj.SetBias(biasGlobal);
    }
    uint8_t enAtomic = 1; // set AtomicAdd
    matmulObj.IterateAll(cGlobal, enAtomic);
    matmulObj.End();
}

__aicore__ inline uint32_t Ceiling(uint32_t a, uint32_t b)
{
    if (b == 0) {
        return 0;
    }
    return (a + b - 1) / b;
}

template <typename aType, typename bType, typename cType, typename biasType>
__aicore__ inline void MatmulSplitkKernel<aType, bType, cType, biasType>::CalcOffset(
    int32_t blockIdx, const TCubeTiling& tiling, int32_t& offsetA, int32_t& offsetB, int32_t& offsetC, int32_t& offsetBias)
{
    auto temp0 = AscendC::Ceil(tiling.M, tiling.singleCoreM);
    auto temp1 = AscendC::Ceil(tiling.N, tiling.singleCoreN);
    auto temp2 = AscendC::Ceil(tiling.Ka, tiling.singleCoreK);

    auto divideKCoreNum = tiling.usedCoreNum / temp2;

    auto mCoreIndex = (blockIdx % divideKCoreNum) % temp0;
    auto nCoreIndex = (blockIdx % divideKCoreNum) / temp0;
    auto subKIndex = blockIdx / divideKCoreNum;

    offsetA = mCoreIndex * tiling.Ka * tiling.singleCoreM + subKIndex * tiling.singleCoreK;
    offsetB = subKIndex * tiling.singleCoreK * tiling.N + nCoreIndex * tiling.singleCoreN;
    offsetC = mCoreIndex * tiling.N * tiling.singleCoreM + nCoreIndex * tiling.singleCoreN;
    offsetBias = nCoreIndex * tiling.singleCoreN;
}
} // namespace MatmulSplitKCustom
#endif // EXAMPLES_MATRIX_MATMUL_SPLITK_OP_KERNEL_MATMUL_SPLITK_CUSTOM_IMPL_H